<?php 
/*
 module:		测试数据控制器
 create_time:	2023-10-17 15:22:03
 author:		your name
 contact:		your email address
*/

namespace app\admin\controller\Demo;
use think\exception\ValidateException;
use app\admin\model\Demo\Mydata as MydataModel;
use app\admin\controller\Admin;
use think\facade\Db;

class Mydata extends Admin {


	/*
	* @Description  数据列表
	*/
	public function index(): \think\response\Json 
	{
		// 分页参数
		$limit  = $this->request->post('limit', 20, 'intval');
		$page = $this->request->post('page', 1, 'intval');

		// 构建查询条件
		$where = [];
		$where['name'] = $this->request->post('name', null, 'serach_in');
		$where['level'] = $this->request->post('level', null, 'serach_in');
		$where['status'] = $this->request->post('status', null, 'serach_in');

		//  查询的字段
		$field = 'mydata_id,name,level,sort_id,status,create_time,update_time';

		//  执行查询操作
		try{
			$res = MydataModel::field($field)
				->where(formatWhere($where))
				->whereRaw($this->whereScope())
				->order('mydata_id desc')
				->paginate(['list_rows'=>$limit,'page'=>$page])
				->toArray();
		} catch (\Exception $e) {
			throw new ValidateException($e->getMessage());
		}
		$data['status'] = 200;
		$data['data'] = $res;
		return json($data);
	}


	/*
 	* @Description  修改排序
 	*/
	public function updateExt(): \think\response\Json 
	{
		$data = $this->request->post();
		if(!$data['mydata_id']) throw new ValidateException ('参数错误');
		MydataModel::update($data);
		return json(['status'=>200,'msg'=>'操作成功']);
	}

	/*
 	* @Description  添加
 	*/
	public function add(): \think\response\Json 
	{
		//  组织提交的数据
		$postField = 'name,level,sort_id,status,create_time';
		$data = $this->request->only(explode(',',$postField),'post',null);
		//  数据校验
		$this->validate($data,\app\admin\validate\Demo\Mydata::class);
		$data['create_time'] = date('Y-m-d H:i:s');
		$data['creater_id'] = $this->userInfo['user_id'];
		$data['creater_dept'] = $this->userInfo['dept_id'];
		// 执行数据操作
		try{
			$res = MydataModel::create($data);
			if($res->mydata_id && empty($data['sort_id'])){
				 MydataModel::update(['sort_id'=>$res->mydata_id,'mydata_id'=>$res->mydata_id]);
			}
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'data'=>$res->mydata_id]);
	}


	/*
 	* @Description  修改
 	*/
	public function update(): \think\response\Json 
	{
		//  组织提交的数据
		$postField = 'mydata_id,name,level,sort_id,status,update_time';
		$data = $this->request->only(explode(',',$postField),'post',null);
		// 执行数据校验
		$this->validate($data,\app\admin\validate\Demo\Mydata::class);
		$data['update_time'] = date('Y-m-d H:i:s');
		// 执行数据操作
		try{
			MydataModel::update($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200]);
	}


	/*
 	* @Description  修改信息之前查询信息的 勿要删除
 	*/
	public function getUpdateInfo(): \think\response\Json 
	{
		$id =  $this->request->post('mydata_id', null, 'serach_in');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'mydata_id,name,level,sort_id,status,update_time';
		$res = MydataModel::field($field)->find($id);
		return json(['status'=>200,'data'=>$res]);
	}


	/*
 	* @Description  删除
 	*/
	public function delete(): \think\response\Json 
	{
		$idx =  $this->request->post('mydata_id', null, 'serach_in');
		if(!$idx) throw new ValidateException ('参数错误');
		MydataModel::destroy(['mydata_id'=>explode(',',$idx)],true);
		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/*
 	* @Description  查看详情
 	*/
	public function detail(): \think\response\Json 
	{
		//  组织提交的数据
		$id =  $this->request->post('mydata_id', null, 'serach_in');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'mydata_id,name,level,sort_id,status,create_time,update_time';
		$res = MydataModel::field($field)->find($id)->toArray();
		return json(['status'=>200,'data'=>$res]);
	}


	/*
 	* @Description  获取定义sql语句的字段信息
 	*/
	public function getFieldList(): \think\response\Json 
	{
		return json(['status'=>200,'data'=>$this->getSqlField('',)]);
	}

	/*
 	* @Description  获取定义sql语句的字段信息
 	*/
	private function getSqlField($list): array 
	{
		$data = [];
		return $data;
	}


	

}

